home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assemblers / cas.lha / hexs / r.a < prev    next >
Encoding:
Text File  |  1992-08-10  |  4.6 KB  |  265 lines

  1. ;;; This particular piece of software is a demo of
  2. ;;; (A) An 8052 multi-tasking architecture
  3. ;;; (B) 9-bit serial communications (in mode 3) at 57600 baud
  4. ;;; (C) A memory dump routine for Intel Hex Format.
  5.  
  6. ;;; It should operate reliably with a host connected over a RS-232, RS-422,
  7. ;;; RS-423 or RS-485.  If using half-duplex, T0 is configured here as the
  8. ;;; Transmit-Enable control output.
  9.  
  10. ;;; Binary Technology 8051/8052 Cross Assembler (sxa51) used.
  11.  
  12. ;;; The registers defined in the 8052.
  13. T2CON  equ 0xc8
  14. RCAP2L equ 0xca
  15. RCAP2H equ 0xcb
  16. TL2    equ 0xcc
  17. TH2    equ 0xcd
  18.  
  19. ;;; Addressible bits
  20. ;;; Port 1
  21. T2   bit P1.0
  22. T2EX bit P1.1
  23.  
  24. ;;; IE and IP
  25. ET2 bit IE.5
  26. PT2 bit IP.5
  27.  
  28. ;;; T2CON
  29. TF2    bit T2CON.7
  30. EXF2   bit T2CON.6
  31. RCLK   bit T2CON.5
  32. TCLK   bit T2CON.4
  33. EXEN2  bit T2CON.3
  34. TR2    bit T2CON.2
  35. C_T2   bit T2CON.1
  36. CP_RL2 bit T2CON.0
  37.  
  38. ;;; An 8052 multi-tasking kernel.
  39. ;;; Process stack
  40. PSP   equ 73h  ;;; int **PSP;
  41. Stack equ 74h  ;;; int *Stack[8];
  42.  
  43. ;;; /* Interrupt descriptor table. */
  44. SP_RI   equ 7ch
  45. SP_TI   equ 7dh
  46. SP_BASE equ 7eh
  47.  
  48. org 00h
  49. sjmp Start
  50.  
  51. org 23h
  52.    jbc TI, DidTx
  53.    jbc RI, DidRx
  54. reti
  55. DidTx:
  56.    mov R0, #SP_TI
  57.    acall Resume
  58. reti
  59. DidRx:
  60.    mov R0, #SP_RI
  61.    acall Resume
  62. reti
  63.  
  64. Start: ;;; Install main(), set its return address to _Die().
  65.    mov PSP, #Stack         ;;; PSP = &Stack[0];
  66.    mov SP, #(SP_BASE - 1)  ;;; SP = SP_BASE - 1;
  67.    mov DPTR, #Exit
  68.    push DPL
  69.    push DPH                ;;; *SP++ = _Exit();
  70.    acall main
  71. _Die:
  72.    orl PCon, #1
  73. sjmp _Die
  74.  
  75. Pause:                     ;;; void Pause(int **R0) {
  76.    mov @R0, SP             ;;;    *R0 = SP;
  77.    dec PSP
  78.    mov R0, PSP
  79.    mov SP, @R0             ;;;    SP = *--PSP;
  80. ret                        ;;;    "idle until resume";
  81. Resume:
  82.    mov R1, PSP
  83.    inc PSP
  84.    mov @R1, SP             ;;;    *PSP++ = SP;
  85.    mov SP, @R0             ;;;    SP = *R0;
  86.    mov @R0, #(SP_BASE + 1) ;;;    *R0 = SP_BASE + 1;
  87. ret                        ;;; }
  88.  
  89. Spawn:                   ;;; void Spawn(int *R0, void *(DPTR())) {
  90.    mov R1, PSP
  91.    inc PSP
  92.    mov @R1, SP           ;;;    *PSP++ = SP;
  93.    dec R0
  94.    mov SP, R0            ;;;    SP = --R0;
  95.    acall _Enter          ;;;    (*DPTR)();
  96. Exit:
  97.    dec PSP
  98.    mov R0, PSP
  99.    mov SP, @R0           ;;;    SP = *--PSP;
  100. ret                      ;;; }
  101. _Enter:
  102.    push DPL
  103.    push DPH
  104. ret
  105.  
  106. ;;; RS-485 SERIAL COMMUNICATIONS.
  107. ;;; Modes:
  108. ;;; (1) Wait for address (REN = 1, T0 = 0, SM2 = 1)
  109. ;;; (2) Wait for data    (REN = 1, T0 = 0, SM2 = 0)
  110. ;;; (3) Send data        (REM = 0, T0 = 1)
  111.  
  112. SER_ADDR equ ':'
  113.  
  114. SetPort:
  115.    mov SCON, #11000000b ;;; Serial comm: 9 bits, no parity, 1 stop bit.
  116.    clr PS
  117.    clr RCLK
  118.    clr TCLK
  119.    mov A, TMOD
  120.    anl A, #0fh
  121.    add A, #00100000b ;;; Timer 1: baud rate timer.
  122.    mov TMOD, A
  123.    mov TH1, #-1         ;;; Baud rate: 28800 baud.
  124.    orl PCON, #80h       ;;; Baud rate now 57600 baud.
  125.    setb TR1
  126.    clr TB8
  127.    clr RI
  128.    clr TI
  129.    setb ES
  130. ret
  131.  
  132. SetRx: ;;; Set mode for "Wait for address"
  133.    clr T0
  134.    setb SM2
  135.    setb REN
  136. ret
  137.  
  138. SetTx: ;;; Set mode for "Send data"
  139.    clr REN
  140.    setb T0
  141. ret
  142.  
  143. getchar:
  144.    getcharLoop:
  145.       mov R0, #SP_RI
  146.       acall Pause
  147.       mov A, SBUF
  148.    jnb SM2, getcharBreak
  149.       cjne A, #SER_ADDR, NoMatch
  150.          clr SM2         ;;; if (A == SER_ADDR) Set mode for "Wait for data";
  151.       NoMatch:
  152.    sjmp getcharLoop
  153.    getcharBreak:
  154. ret
  155.  
  156. nl:
  157.    mov A, #13
  158.    acall putchar
  159.    mov A, #10
  160. putchar:
  161.    mov SBUF, A
  162.    mov R0, #SP_TI
  163.    acall Pause
  164. ret
  165.  
  166. ;;; APPLICATION
  167. putnib:
  168.    anl A, #0fh
  169.    cjne A, #10, $ + 3
  170. jc isdigit
  171.    add A, #('a' - 10)
  172.    acall putchar
  173. ret
  174. isdigit:
  175.    add A, #'0'
  176.    acall putchar
  177. ret
  178.  
  179. puthex:
  180.    xch A, R3
  181.    add A, R3
  182.    xch A, R3
  183.    push ACC
  184.    swap A
  185.    acall putnib
  186.    pop ACC
  187.    acall putnib
  188. ret
  189.  
  190. putend:
  191.    mov A, #':'
  192.    acall putchar
  193.    mov R3, #0
  194.    mov A, #00h
  195.    acall puthex
  196.    mov A, DPH
  197.    acall puthex
  198.    mov A, DPL
  199.    acall puthex
  200.    mov A, #1
  201.    acall puthex
  202.    mov A, R3
  203.    cpl A
  204.    acall puthex
  205.    acall nl
  206. ret
  207.  
  208. putdata:
  209.    mov A, #':'
  210.    acall putchar
  211.    mov R3, #0
  212.    mov A, #10h
  213.    acall puthex
  214.    mov A, DPH
  215.    acall puthex
  216.    mov A, DPL
  217.    acall puthex
  218.    mov A, #0
  219.    acall puthex
  220.    mov R2, #10h
  221.    DataLoop:
  222.       clr A
  223.       movc A, @A + DPTR
  224.       inc DPTR
  225.       acall puthex
  226.    djnz R2, DataLoop
  227.    mov A, R3
  228.    cpl A
  229.    acall puthex
  230.    acall nl
  231. ret
  232.  
  233. hexs:
  234.    mov DPTR, #0000h
  235.    hexsLoop:
  236.    mov A, DPH
  237.    cjne A, #20h, $ + 3
  238.    jnc hexsBreak
  239.       acall putdata
  240.    sjmp hexsloop
  241. hexsBreak:
  242.    acall putend
  243. ret
  244.  
  245. Echo:
  246.    acall SetPort
  247. EchoLoop:
  248.    acall SetRx
  249.    acall getchar
  250. X0: cjne A, #'s', X1
  251.    acall hexs
  252. sjmp EchoLoop
  253. X1:
  254.    acall SetTx
  255.    acall putchar
  256. sjmp EchoLoop
  257.  
  258. main:
  259.    mov R0, #90h
  260.    mov DPTR, #Echo
  261.    acall Spawn
  262.    setb EA
  263. ret
  264. end
  265.